package com.sip.db.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import com.sip.db.dao.DeviceRepository;
import com.sip.db.entity.DeviceEntity;
import com.sip.db.service.DeviceService;

@Service
public class DeviceServiceImpl implements DeviceService {
	@Autowired
	DeviceRepository deviceRepository;
	
	@Override
	public DeviceEntity saveOrUpdate(DeviceEntity deviceEntity) {
		return deviceRepository.save(deviceEntity);
	}

	@Override
	public DeviceEntity findOne(DeviceEntity deviceEntity) {
		Example<DeviceEntity> example = Example.of(deviceEntity);
		Optional<DeviceEntity> optional = deviceRepository.findOne(example);
		if (optional.isPresent()) {
			DeviceEntity device = optional.get();
			return device;
		} else {
			return null;
		}
	}

	@Override
	public List<DeviceEntity> listAll(DeviceEntity deviceEntity) {
		Example<DeviceEntity> example = Example.of(deviceEntity);
		List<DeviceEntity> deviceList = deviceRepository.findAll(example);
		return deviceList;
	}

	@Override
	public void delete(DeviceEntity deviceEntity) {
		deviceRepository.delete(deviceEntity);
	}

}
